/*
 * Vector3D.java
 *
 * Created on 4 de Dezembro de 2007, 10:56
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package mathematics;


/**
 *
 * @author rafaelbuck
 */
public final class Vector3D
{

    /** Creates a new instance of Vector3D */
    public Vector3D()
    {
    }

    /** Creates a new instance of Vector3D */
    public Vector3D(Vector3D v)
    {
        x = v.x;
        y = v.y;
        z = v.z;
    }

    /** Creates a new instance of Vector3D */
    public Vector3D(Point3D p)
    {
        x = p.x;
        y = p.y;
        z = p.z;
    }

    /** Creates a new instance of Vector3D */
    public Vector3D(float xx, float yy, float zz)
    {
        x = xx;
        y = yy;
        z = zz;
    }

    public float length()
    {
        return (float)Math.sqrt(x * x + y * y + z * z);
    }

    public void scale(float f)
    {
        if(f != 1.0F)
        {
            x *= f;
            y *= f;
            z *= f;
        }
    }

    public void normalize()
    {
        scale(1.0F / length());
    }

    public void add(Vector3D v)
    {
        x += v.x;
        y += v.y;
        z += v.z;
    }

    public Vector3D plus(Vector3D v)
    {
        Vector3D ret = new Vector3D(this);
        ret.add(v);
        return ret;
    }

    public Point3D plus(Point3D p)
    {
        Point3D ret = new Point3D(p);
        ret.add(this);
        return ret;
    }

    public void sub(Vector3D v)
    {
        x -= v.x;
        y -= v.y;
        z -= v.z;
    }

    public Point3D minus(Point3D p)
    {
        Point3D ret = new Point3D(p);
        ret.sub(this);
        ret.scale(-1F);
        return ret;
    }

    public Vector3D minus(Vector3D v)
    {
        Vector3D ret = new Vector3D(this);
        ret.sub(v);
        return ret;
    }

    public float mult(Vector3D v)
    {
        return x * v.x + y * v.y + z * v.z;
    }

    public Vector3D cross(Vector3D v)
    {
        return new Vector3D(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
    }

    public boolean equals(Vector3D v)
    {
        return v.x == x && v.y == y && v.z == z;
    }

    public String toString()
    {
        return new String((new StringBuffer()).append("<").append(x).append(",").append(y).append(",").append(z).append(">"));
    }

    public float x;
    public float y;
    public float z;
}